#coding=utf-8

class FileUtil:

    def __init__(self):
        # 读取模板的路径
        self.template_path = "static/template2.html"
        # 结果输出路径
        self.output_file_path = "static/output.html"

    # 拼接模板内容跟爬取内容，形成最后输出
    def get_final_output(self, temp_list, cont_list, title_list):
        final_cont = ""
        for i, tem in enumerate(temp_list):
            if (i > len(cont_list) - 1):
                break
            k = i + 1
            tem = tem.replace('标题' + str(k), title_list[i])
            line = tem + cont_list[i]
            final_cont += line
        self.write_file(final_cont)

    # 读取模板内容
    def read_template_2_list(self):
        html_file = open(self.template_path, 'r', encoding="utf-8")
        html_cont = html_file.read()
        print(html_cont)
        temp_list = html_cont.split('placeholder')
        return temp_list

    # 写入文件,会删除原文件内容,文件不存在将创建
    def write_file(self, cont):
        fo = open(self.output_file_path, "w", encoding="utf-8")
        fo.write(cont)
        fo.close()

fileUtil = FileUtil()